//
//  SVLoginViewController.m
//  ShortVideo-ccinon
//
//  Created by LIN TONG on 2017/6/19.
//  Copyright © 2017年 LIN TONG. All rights reserved.
//

#import "SVLoginViewController.h"
#import "SVConfig.h"
#import "SVListViewController.h"
#import "SVAPIReqeust.h"
#import "SVLoginManager.h"

@interface SVLoginViewController ()<UITextFieldDelegate>{
    UITextField *accountField;
    UITextField *passwordField;
    UIView      *contentView;
}

@end

@implementation SVLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorMake(239, 239, 239, 1);
    
    contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, __SCREEN_WIDTH__, __SCREEN_HEIGHT__)];
    contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:contentView];
    // Do any additional setup after loading the view.
    
    UIImageView *logo = [[UIImageView alloc]initWithFrame:CGRectMake(S(48), S(40), S(224), S(256))];
    logo.image = [UIImage imageNamed:@"logo"];
    [contentView addSubview:logo];
    
    
    UIView *accountView = [[UIView alloc]initWithFrame:CGRectMake(S(20), S(316), __SCREEN_WIDTH__-S(40), S(44))];
    accountView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:accountView];
    
    UILabel *accountTitle = [[UILabel alloc]initWithFrame:CGRectMake(S(10), 0, S(60), CGRectGetHeight(accountView.frame))];
    accountTitle.font = Font(14);
    accountTitle.textColor = UIColorMake(30, 30, 30, 1);
    accountTitle.text = @"手机号";
    [accountView addSubview:accountTitle];
    
    accountField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(accountTitle.frame)+S(20), 0, CGRectGetWidth(accountView.frame)-CGRectGetMaxX(accountTitle.frame)-S(30), CGRectGetHeight(accountView.frame))];
    accountField.delegate = self;
    accountField.returnKeyType = UIReturnKeyDone;
    accountField.font = Font(14);
    accountField.keyboardType = UIKeyboardTypeNumberPad;
    accountField.placeholder = @"您的手机号";
    [accountView addSubview:accountField];
    
    NSString *account = [JNKeychain loadValueForKey:SV_USER_NAME];
    if(account && account.length>0)accountField.text = account;
    
    UIView *horLine = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(accountView.frame), CGRectGetMaxY(accountView.frame), CGRectGetWidth(accountView.frame), S(1))];
    horLine.backgroundColor = UIColorMake(46, 46, 46, 1);
    [contentView addSubview:horLine];
    
    UIView *passwordView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(accountView.frame), CGRectGetMaxY(horLine.frame), CGRectGetWidth(accountView.frame), CGRectGetHeight(accountView.frame))];
    passwordView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:passwordView];
    
    UILabel *passwordTitle = [[UILabel alloc]initWithFrame:CGRectMake(S(10), 0, S(60), CGRectGetHeight(passwordView.frame))];
    passwordTitle.font = Font(13);
    passwordTitle.textColor = UIColorMake(30, 30, 30, 1);
    passwordTitle.text = @"密码";
    [passwordView addSubview:passwordTitle];
    
    passwordField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(passwordTitle.frame)+S(20),0, CGRectGetWidth(passwordView.frame)-CGRectGetMaxX(passwordTitle.frame)-S(30), CGRectGetHeight(passwordView.frame))];
    passwordField.returnKeyType = UIReturnKeyDone;
    passwordField.delegate = self;
    passwordField.secureTextEntry = YES;
    passwordField.font = Font(14);
    passwordField.placeholder = @"您的密码";
    [passwordView addSubview:passwordField];
    
    NSString *password = [JNKeychain loadValueForKey:SV_USER_PASSWORD];
    if(password && password.length>0) passwordField.text = password;
    
    UIButton *loginButton = [[UIButton alloc]initWithFrame:CGRectMake(S(90), S(428), S(140), S(35))];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginButton.layer.cornerRadius = S(5);
    loginButton.backgroundColor = UIColorMake(249, 84, 0, 1);
    loginButton.layer.masksToBounds = YES;
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:S(16)];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(clickToLogin:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:loginButton];
    
    
    UIButton *contactUs = [[UIButton alloc]initWithFrame:CGRectMake(S(90), S(504), S(140), S(22))];
    contactUs.titleLabel.font = Font(13);
    contactUs.layer.cornerRadius = S(3);
    contactUs.layer.masksToBounds = YES;
    contactUs.layer.borderWidth = 1;
    contactUs.layer.borderColor = UIColorMake(30, 30, 30, 1).CGColor;
    [contactUs setTitle:@"联系我们" forState:UIControlStateNormal];
    [contactUs setTitleColor:UIColorMake(100, 100, 100, 1) forState:UIControlStateNormal];
    [contactUs addTarget:self action:@selector(contactUs:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:contactUs];
    
    UILabel *copyright = [[UILabel alloc]initWithFrame:CGRectMake(0, S(546), __SCREEN_WIDTH__, S(15))];
    copyright.textAlignment = NSTextAlignmentCenter;
    copyright.font = Font(12);
    copyright.textColor = UIColorMake(100, 100, 100, 1);
    copyright.text = @"copyright@ccinon";
    [contentView addSubview:copyright];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickToLogin:(UIButton *)sender{
    [SVAPIReqeust svRequestAPI:API(@"/mct/admin/me/login") method:@"POST"
                    parametors:@{@"cpn_user_mobile":accountField.text,
                                 @"cpn_user_passwd":passwordField.text}
               completionBlock:^(NSString *requestURL, BOOL success, NSDictionary *data, NSError *error) {
                   if(success){
                       
                       SVLoginManager *manager = [SVLoginManager shareLoginManager];
                       manager.status = SVLoginStatusLogin;
                       
                       NSDictionary *jsonData = [data objectForKey:@"data"];
                       
                       APPDELEGATE.company = [[SVCompany alloc]init];
                       
                       APPDELEGATE.company.companyID = [[jsonData objectForKey:@"cpn_id"]integerValue];
                       APPDELEGATE.company.companyName = [jsonData objectForKey:@"realname"];
                       APPDELEGATE.company.mobilePhoneNumber = [jsonData objectForKey:@"mobile"];
                       APPDELEGATE.company.createTime = [jsonData objectForKey:@"add_time"];
                       
                       //存储用户信息到keychain
                       [JNKeychain saveValue:accountField.text forKey:SV_USER_NAME];
                       [JNKeychain saveValue:passwordField.text forKey:SV_USER_PASSWORD];
                       
                       SVListViewController *listVC = [[SVListViewController alloc]init];
                       [self.navigationController pushViewController:listVC animated:YES];
                   }
    }];
    
}

- (void)contactUs:(UIButton *)sender{
    NSString *phone = @"tel:13540715911";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phone]];
}

- (void)scrollViewToTop:(BOOL)flag{
    if(flag){
        [UIView animateWithDuration:0.3 animations:^{
            contentView.frame = CGRectMake(0, -S(130), __SCREEN_WIDTH__, __SCREEN_HEIGHT__);
        } completion:^(BOOL finished) {
            
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            contentView.frame = CGRectMake(0, 0, __SCREEN_WIDTH__, __SCREEN_HEIGHT__);
        } completion:^(BOOL finished) {
            
        }];
    }
}

#pragma mark - TextField Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self scrollViewToTop:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self scrollViewToTop:NO];
    [textField resignFirstResponder];
    return YES;
}

@end
